package com.qtenv.datasync.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qtenv.datasync.service.entity.Realtimegatherdata;
import com.qtenv.datasync.service.entity.Terminals_bf;
import com.qtenv.datasync.service.entity.TerminalsXb;
import com.qtenv.datasync.service.mapper.TerminalsMapper;
import com.qtenv.datasync.service.service.RealtimegatherdataService;
import com.qtenv.datasync.service.service.TerminalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CBL
 * @since 2022-06-22
 */
@Service
@DS("master")
public class TerminalsServiceImpl extends ServiceImpl<TerminalsMapper, Terminals_bf> implements TerminalsService {

    @Autowired
    private RealtimegatherdataService realtimegatherdataService;

    @Override
    @DS("middle")
    public void saveToMiddle(List<Terminals_bf> terminalsList) {
        this.saveBatch(terminalsList);
    }

    @Override
    @DS("middle")
    public List<Terminals_bf> findMidTerminalsByTenantAndSupplier(String tenant, Integer supplier) {
        return this.list(new QueryWrapper<Terminals_bf>().eq("tenant_id", tenant)
                .likeLeft("SupplierID", supplier));
    }

    @Override
    public List<Terminals_bf> findMasTerminalsByTenantAndSupplier(String tenant, Integer supplier) {
        List<Terminals_bf> result = this.list(new QueryWrapper<Terminals_bf>().eq("tenant_id", tenant)
                .eq("SupplierID", supplier).eq("TerminalMark", 1));
        if (CollectionUtil.isNotEmpty(result)) {
            result.forEach(item -> item.setTerminalID(item.getTerminalID().substring(10)));
        }
        return result;
    }
    @Override
    public List<Terminals_bf> findMasTerminalsByTenantAndSupplier_new(String tenant, Integer supplier) {
        List<Terminals_bf> result = this.getBaseMapper().selectList(new QueryWrapper<Terminals_bf>().eq("tenant_id", tenant)
                .eq("SupplierID", supplier).eq("TerminalMark", 1));
       /* if (CollectionUtil.isNotEmpty(result)) {
            result.forEach(item -> item.setTerminalID(item.getTerminalID().substring(10)));
        }*/
        return result;
    }

    @Override
    public List<Terminals_bf> findMasTerminalsByTenantAndSupplierAndLastTime(String tenant, Integer supplier) {
        return this.getBaseMapper().findMasTerminalsByTenantAndSupplierAndLastTime(tenant, String.valueOf(supplier));
    }

    @Override
    public List<Terminals_bf> getTerminals(String tenantId) {
        List<Terminals_bf> terminalsListList = this.list(new QueryWrapper<Terminals_bf>().eq("tenant_id", tenantId)
                .eq("TerminalMark", 1).isNotNull("Lng").ne("Lng", "")
                .isNotNull("Lat").ne("Lat", ""));

        if (CollectionUtil.isNotEmpty(terminalsListList)) {
            List<String> terminalIdList = terminalsListList.stream().map(item -> item.getTerminalID()).collect(Collectors.toList());
            List<Realtimegatherdata> terminalHistoryDataList = realtimegatherdataService.list(new QueryWrapper<Realtimegatherdata>().in("TerminalID", terminalIdList));
            terminalsListList.forEach(terminal -> {
                Realtimegatherdata realtimegatherdata = terminalHistoryDataList.stream().filter(item -> item.getTerminalID().equals(terminal.getTerminalID())).findFirst().orElse(null);
                terminal.setRealtimedata(realtimegatherdata);
            });
        }

        return terminalsListList;
    }

    @Override
    public Terminals_bf getTerminalBasicInfo(String tenantId, String terminalId) {
        return this.getOne(new QueryWrapper<Terminals_bf>().eq("tenant_id", tenantId).eq("TerminalID", terminalId));
    }

    @Override
    public Page<TerminalsXb> getAllTerminalDataList(String tenantId, String terminalName, String terminalInstallAddr, String page, String pageSize) {
        QueryWrapper<Terminals_bf> query = new QueryWrapper<Terminals_bf>().eq("tenant_id", tenantId).eq("TerminalMark", 1);

        if (StrUtil.isNotEmpty(terminalName)) {
            query = query.like("TerminalName", terminalName);
        }

        if (StrUtil.isNotEmpty(terminalInstallAddr)) {
            query = query.like("TerminalInstallAddr", terminalInstallAddr);
        }

        return this.getBaseMapper().selectPage(new Page(Integer.valueOf(page), Integer.valueOf(pageSize)), query);
    }
}
